package com.example.arhosp;

import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.metaio.sdk.GestureHandlerAndroid;
import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.jni.GestureHandler;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.TrackingValuesVector;
import com.metaio.sdk.jni.Vector3d;
import com.metaio.tools.io.AssetsManager;

public class AR_doctor extends MetaioSDKViewActivity {
	
	private IGeometry mBoton6,mBoton7, mBoton8;
	String trackingConfigFile;
	
	private MetaioSDKCallbackHandler mCallbackHandler;
	
	private GestureHandlerAndroid mGestureHandler;
	
	//@Override
	//protected IMetaioSDKCallback getMetaioSDKCallbackHandler1(){
		//return mCallbackHandler;
	//}
	
	@Override
	public boolean onTouch(View v, MotionEvent event){
		super.onTouch(v, event);
		mGestureHandler.onTouch(v, event);
		return true;
		
	}
	
	
	@Override
	protected void onStart(){
		super.onStart();
		
		if(!mRendererInitialized)
			mGUIView.setVisibility(View.GONE);
		
	}

	final class MetaioSDKCallbackHandler extends IMetaioSDKCallback{
		@Override
		public void onSDKReady() 
		{
			// show GUI
			runOnUiThread(new Runnable() 
			{
				@Override
				public void run() 
				{
					mGUIView.setVisibility(View.VISIBLE);
				}
			});
		}
	}
	
	@Override
	public void onDrawFrame(){
		super.onDrawFrame();
		
		if(metaioSDK!=null){
			//obtener todas los targets
			TrackingValuesVector poses=metaioSDK.getTrackingValues();
			
		
			if(poses.size()!=0){
				
				mBoton6.setCoordinateSystemID(1);//poses.get(1).getCoordinateSystemID());
				mBoton7.setCoordinateSystemID(1);//poses.get(1).getCoordinateSystemID());
				mBoton8.setCoordinateSystemID(1);//poses.get(1).getCoordinateSystemID());
				
			}
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		mCallbackHandler =new MetaioSDKCallbackHandler();
		//String trackingConfigFile1=AssetsManager.getAssetPath("TrackingData_PictureMarker.xml");
		//MetaioDebug.log("Tracking Config path = "+trackingConfigFile1);
		
		//boolean result1 = metaioSDK.setTrackingConfiguration(trackingConfigFile1); 
		//MetaioDebug.log("Picture Marker tracking data loaded: " + result1);
		
	}
	
	
	@Override
	protected int getGUILayout() 
	{
		// Attaching layout to the activity
		return R.layout.ar_doctores; 
	}


	public void onButtonClick(View v)
	{
		finish();
	}
	
	public void onPictureButtonClick(View v)
	{
		String trackingConfigFile = AssetsManager.getAssetPath("TrackingData_PictureMarker.xml");
		
		MetaioDebug.log("Tracking Config path = "+trackingConfigFile);
		
		boolean result = metaioSDK.setTrackingConfiguration(trackingConfigFile); 
		MetaioDebug.log("Picture Marker tracking data loaded: " + result); 
		//mModel.setScale(new Vector3d(8f, 8f, 8f));

	}
	
	@Override
	protected void loadContent() 
	{
		try
		{
			
			
			// Load desired tracking data for planar marker tracking
						trackingConfigFile = AssetsManager.getAssetPath("TrackingData_PictureMarker1.xml");
						//trackingConfigFile = AssetsManager.getAssetPath("TrackingData_BarCode.xml");
						
						boolean result = metaioSDK.setTrackingConfiguration(trackingConfigFile); 
						MetaioDebug.log("Markerless tracking data loaded: " + result); 
				        
						//botones
						
						String mModel1 = AssetsManager.getAssetPath("Servicios.zip");			
						if (mModel1 != null) 
						{
							mBoton6 = metaioSDK.createGeometry(mModel1);
							if (mBoton6 != null) 
							{
								// Set geometry properties
								mBoton6.setScale(new Vector3d(10.0f, 10.0f, 10.0f));
								mBoton6.setTranslation(new Vector3d(-10,0 , 0));
								MetaioDebug.log("Loaded geometry "+mModel1);
							}
							else
								MetaioDebug.log(Log.ERROR, "Error loading geometry: "+mModel1);
						}
						
						
						String mModel2 = AssetsManager.getAssetPath("Medicamentos.zip");			
						if (mModel2 != null) 
						{
							mBoton7 = metaioSDK.createGeometry(mModel2);
							if (mBoton7 != null) 
							{
								// Set geometry properties
								mBoton7.setScale(new Vector3d(10.0f, 10.0f, 10.0f));
								mBoton7.setTranslation(new Vector3d(135,0, 0));
								MetaioDebug.log("Loaded geometry "+mModel2);
							}
							else
								MetaioDebug.log(Log.ERROR, "Error loading geometry: "+mModel2);
						}
						
						String mModel3 = AssetsManager.getAssetPath("boton3.zip");			
						if (mModel3 != null) 
						{
							mBoton8 = metaioSDK.createGeometry(mModel3);
							if (mBoton8 != null) 
							{
								// Set geometry properties
								mBoton8.setScale(new Vector3d(15.0f, 15.0f, 15.0f));
								mBoton8.setTranslation(new Vector3d(0,100,0));
								MetaioDebug.log("Loaded geometry "+mModel3);
							}
							MetaioDebug.log(Log.ERROR, "Error loading geometry: "+mModel3);
						}

						mGestureHandler=new GestureHandlerAndroid(metaioSDK,GestureHandler.GESTURE_PINCH);
						mGestureHandler.addObject(mBoton6, 1, true);
						mGestureHandler.addObject(mBoton7, 1, true);
						mGestureHandler.addObject(mBoton8, 1, true);	
			
		}       
		catch (Exception e)
		{
			
		}
	}
	
  
	@Override
	protected void onGeometryTouched(IGeometry geometry) {
		// TODO Auto-generated method stub
		MetaioDebug.log("UnifeyeCallbackHandler.onGeometryTouched: "+geometry);
		
		
		if(geometry.equals(mBoton6)){//valida si se toca boton 1
			try{
				String info="http://www.hospitalortopedia.gob.gt/infhos/web/ui/frmenfermeria2.aspx?idServicio=4";
				Uri uri=Uri.parse(info);
				Intent in=new Intent(Intent.ACTION_VIEW,uri);
				startActivity(in);
			}catch(Exception e){
				
			}
		}
		
		if(geometry.equals(mBoton7)){//valida si se toca boton 1
			try{
				String info="http://www.hospitalortopedia.gob.gt/infhos/web/ui/frmServicioDieta.aspx?idServicio=4";
				Uri uri=Uri.parse(info);
				Intent in=new Intent(Intent.ACTION_VIEW,uri);
				startActivity(in);
			}catch(Exception e){
				
			}
		}
		
		if(geometry.equals(mBoton8)){//valida si se toca boton 1
			try{
				String info="http://www.hospitalortopedia.gob.gt/infhos/web/ui/frmServicioMedico.aspx?idServicio=4";
				Uri uri=Uri.parse(info);
				Intent in=new Intent(Intent.ACTION_VIEW,uri);
				startActivity(in);
			}catch(Exception e){
				
			}
		}
	}


	@Override
	protected IMetaioSDKCallback getMetaioSDKCallbackHandler() {
		// TODO Auto-generated method stub
		return mCallbackHandler;
	}
	

}
